package com.lkk.cg.base;

import com.lkk.cg.domain.User;
import com.lkk.cg.service.*;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.annotation.Resource;
import java.lang.reflect.ParameterizedType;

/**
 * Created by lkk on 2014/11/27.
 */
public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {

    protected Log log = LogFactory.getLog(getClass());


    public BaseAction() {
        ParameterizedType typeInfo = (ParameterizedType) this.getClass().getGenericSuperclass();
        Class clazz = (Class) typeInfo.getActualTypeArguments()[0];
        try {
            model = (T) clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }


    //===============ModelDriven===================

    protected T model;

    public T getModel() {
        return model;
    }


    //===============Service=======================
    @Resource
    protected UserService userService;
    @Resource
    protected RoleService roleService;
    @Resource
    protected PrivilegeService privilegeService;
    @Resource
    protected ArticleService articleService;
    @Resource
    protected ReplyService replyService;
    @Resource
    protected DailyStatisticsService dailyStatisticsService;


    //============== Page info =====================
    protected int pageNum;
    protected int pageSize;

    public int getPageNum() {
        return pageNum == 0 ? 1 : pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    //todo 部署时修改为10条每页
    public int getPageSize() {
        return pageSize == 0 ? 10 : pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    //=============== Other =======================
    protected User getCurrentUser() {
        Object currentUser = ActionContext.getContext().getSession().get("user");
        return currentUser == null ? null : (User) currentUser;
    }


}
